package makaveli.listview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyAdapter<T> extends BaseAdapter implements OnClickListener{
	private T[] _insideArr;
	private Context _context;
	private LayoutInflater _layInfl;
	private View view;
	
	public MyAdapter(Context context, T[] arr) {
		_insideArr = arr;
		_context = context;
		_layInfl = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d("MyAdapter", "position: " + position + ", convertView: " + convertView);
		//if (position % 2 != 0)
			view = _layInfl.inflate(R.layout.my_item_layout, null);
		/*else
			view = _layInfl.inflate(R.layout.my_item_layout_even, null);*/
		
		((TextView) view.findViewById(R.id.peopleName)).setText(((People)_insideArr[position])._name);
		((TextView) view.findViewById(R.id.peopleAge)).setText(String.valueOf(((People)_insideArr[position])._age));
		
		((TextView) view.findViewById(R.id.peopleSurname)).setText(((People)_insideArr[position])._surName);
		((TextView) view.findViewById(R.id.peopleSalary)).setText(String.valueOf(((People)_insideArr[position])._salary));
		
		Button butt = (Button) view.findViewById(R.id.peopleButton);
		butt.setText(String.valueOf(position));
		butt.setTag(String.valueOf(position));
		butt.setOnClickListener(this);
		
		Log.d("MyAdapter", "position: " + position + ", view: " + view);
		return view;
	}

	@Override
	public int getCount() {
		return _insideArr.length;
	}

	@Override
	public Object getItem(int arg0) {
		return _insideArr[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}
	
	public void onClick(View v) {
		Toast.makeText(_context, "Position is: " + v.getTag(), Toast.LENGTH_SHORT).show();
	}
}